<?php

/**
 * Estado
 *  
 * @author Tiago Santos
 * @version 
 */

class Estado {
	private $table;
	
	public function getDbTable(){
		if($this->table === NULL){
			require_once APPLICATION_PATH . '/models/DbTable/Estado.php';
			$this->table = new EstadoDbTable();
		}
		return $this->table;
	}
	
	public function getAll(){
		$dt = $this->getDbTable();
		$select = $dt->select()->order('ds_uf');
		$rows = $dt->fetchAll($select)->toArray();
		return $rows;
	}
	
	public function getSelect(){
		$regs = $this->getAll();
		$arr = array();
		foreach($regs as $r){
			$arr[$r['cd_estado']] = $r['ds_uf'];
		}
		return $arr;
	}
	
	public function getById($codigo){
		$dt = $this->getDbTable();
		$select = $dt->select()->where('cd_estado = ' . $codigo);
		$rows = $dt->fetchAll($select);
		$rows = $rows->toArray();
		return $rows[0];
	}
	
	public function save($array){
		if(trim($array['cd_estado'])!='')
			$this->update($array,"cd_estado = " . $array['cd_estado']);
		else{
			unset($array['cd_estado']);
			$this->getDbTable()->insert($array);
		}
	}
	
	public function update($array,$where){
		$this->getDbTable()->update($array,$where);
	}
	
	public function delete($where){
		$this->getDbTable()->delete($where);
	}
}
